﻿using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Stats.Dtos;
using Abp.Domain.Entities.Auditing;
using Yozeev.Controllers.Stats.Oper;
using Abp.UI;

namespace Yozeev.Controllers.Stats
{
    /// <summary>
    /// 首页接口
    /// </summary>
    [Route("api/Stats/[Controller]/[Action]")]
    public class OperController: YozeevControllerBase
    {
        private readonly IRepository<Activity, Guid> _activityRep;

        private readonly IRepository<TicketDetail, Guid> _ticketDetailRep;

        private readonly IRepository<Ticket, Guid> _ticketRep;

        private readonly IRepository<Schedule, Guid> _scheduleRep;

        private IRevenueAnalytics revenueAnalytics;

        private ITimeSpan timeSpan;

        public OperController(
            IRepository<Activity, Guid> activityRep,
            IRepository<TicketDetail, Guid> ticketDetailRep, 
            IRepository<Ticket, Guid> ticketRep,
            IRepository<Schedule, Guid> scheduleRep)
        {
            _scheduleRep = scheduleRep;
            _ticketRep = ticketRep;
            _ticketDetailRep = ticketDetailRep;
            _activityRep = activityRep;
        }

        /// <summary>
        /// 营收状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<RevenueAnalyticsResultDto> RevenueAnal(RevenueAnalInput input)
        {
            AnalyticType type = input.type;
            string date = input.date;
            switch (type)
            {
                case AnalyticType.Monthly:
                    timeSpan = new MonthlyTimeSpan(_activityRep);
                    revenueAnalytics = new RevenueAnalytics(_activityRep);
                    return revenueAnalytics.Analycis(timeSpan, date, 30);
                case AnalyticType.Weekly:
                    timeSpan = new WeeklyTimeSpan(_activityRep);
                    revenueAnalytics = new RevenueAnalytics(_activityRep);
                    return revenueAnalytics.Analycis(timeSpan, date, 7);
                case AnalyticType.Yearly:
                    timeSpan = new YearlyTimeSpan(_activityRep);
                    revenueAnalytics = new RevenueAnalytics(_activityRep);
                    return revenueAnalytics.Analycis(timeSpan, date, 12);
                case AnalyticType.Daily:                    
                default:
                    timeSpan = new DailyTimeSpan(_activityRep);
                    revenueAnalytics = new RevenueAnalytics(_activityRep);                    
                    return revenueAnalytics.Analycis(timeSpan, date, 24);
            }
        }

        /// <summary>
        /// 票型比例
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TicketRatesResult>> TicketRates(AnalyticType type)
        {
            var ticketdetails = _ticketDetailRep.GetAll();
            DateTime today = DateTime.Now;
            DateTime startDateTime;
            DateTime endDateTime;
            switch (type)
            {
                case AnalyticType.Weekly:
                    startDateTime = today.AddDays(-7).AddDays(1);
                    endDateTime = today.AddDays(1).AddTicks(-1);
                    break;
                case AnalyticType.Monthly:
                    startDateTime = new DateTime(today.Year, today.Month, 1);
                    endDateTime = startDateTime.AddMonths(1).AddTicks(-1);
                    break;
                case AnalyticType.Yearly:
                    startDateTime = new DateTime(today.Year, 1, 1);//Today at 00:00:00
                    endDateTime = today.AddYears(1).AddTicks(-1);
                    break;
                case AnalyticType.Daily:
                default:
                    startDateTime = today; //Today at 00:00:00
                    endDateTime = today.AddDays(1).AddTicks(-1);
                    break;
            }

            ticketdetails = ticketdetails.Where(td => td.StartDateTime >= startDateTime && td.StartDateTime <= endDateTime);
            var tickets = _ticketRep.GetAll();

            var result = from td in ticketdetails
                         join t in tickets
                         on td.TicketId equals t.Id
                         group t by new { t.Id, t.TicketName } into g
                         select new TicketRatesResult
                         {
                             TicketTypeId = g.Key.Id,
                             TicketTypeName = g.Key.TicketName,
                             Count = g.Count()
                         };
            return result.ToList();
        }

        /// <summary>
        /// 可售航班信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<ScheduleAnalResultDto>> ScheduleInfo(string startDate, string endDate)
        {
            if (String.IsNullOrEmpty(startDate) || String.IsNullOrEmpty(endDate))
            {
                throw new UserFriendlyException(200, "日期不能为空");
            }

            DateTime startDateTime, endDateTime;
            try
            {
                var date1 = DateTime.Parse(startDate);
                var date2 = DateTime.Parse(endDate);
                startDateTime = new DateTime(date1.Year, date1.Month, date1.Day);
                endDateTime = new DateTime(date2.Year, date2.Month, date2.Day).AddDays(1).AddTicks(-1);
            }
            catch (Exception)
            {
                throw new UserFriendlyException(200, "时间格式不正确");
            }

            var schedules = _scheduleRep.GetAll()
                .Where(s => s.StartTime >= startDateTime && s.StartTime <= endDateTime)
                .Select(s => new ScheduleAnalResultDto 
                { 
                    Schedule = _scheduleRep.GetAll().Where(sc => sc.Id == s.Id).FirstOrDefault(), 
                    Boat = s.Boat, 
                    Route = s.Route 
                });

            return schedules.ToList();
        }

        /// <summary>
        /// 游船列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<BoatRunningStatusDto>> BoatInfo()
        {
            var startDateTime = DateTime.Now;
            var endDateTime = startDateTime.AddDays(1).AddTicks(-1);
            var schedules = _scheduleRep.GetAll()
                .Where(s => s.StartTime >= startDateTime && s.StartTime <= endDateTime)
                .Select(s => new BoatRunningStatusDto 
                {
                    Schedule = _scheduleRep.GetAll().Where(sc => sc.Id == s.Id).FirstOrDefault(),
                    Boat = s.Boat, 
                    Saled = s.SeatQuantity - s.SurplusQuantity, 
                    Rate = ((s.SeatQuantity - s.SurplusQuantity) * 100) + "%" 
                });

            return schedules.ToList();
        }
    }
}
